package com.daotonginfo.generator.model;

import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import com.google.common.base.CaseFormat;


@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class TableInfo {
    private String tableName;
    private List<Column> columnList;
    private String comment;
    private String className;
    /**
     * java 驼峰名
     */
    private String camelName;

    private String hyphenName;

    private String prefix;

    public String getClassName() {
        removePrefix();
        return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, tableName);
//        return tableName.substring(0, 1).toUpperCase() + StringUtil.underlineToCamel(tableName.substring(1));
    }

    public String getHyphenName() {
        removePrefix();
        return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_HYPHEN, tableName);
    }

    private void removePrefix() {
        if (prefix.contains(",")) {
            String[] pre = prefix.split(",");
            for (String s : pre) {
                //替换表名前缀。
                if (tableName.startsWith(s)) {
                    tableName = tableName.replaceFirst(s, "");
                }
            }
        } else {
            if (tableName.startsWith(prefix)) {
                tableName = tableName.replaceFirst(prefix, "");
            }
        }
    }

    public String getCamelName() {
        removePrefix();
//        return StringUtil.underlineToCamel(tableName.substring(1));
        return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, tableName);
    }
}
